<?php
class Admin_Model_Users extends Zend_Db_Table{
	protected $_name = 'users';
	protected $_primary = 'user_id';
	/* Link tham khao : http://framework.zend.com/manual/en/zend.db.table.html */
	////////////
	/* select*/
	//////////
	public function selectALLUser()
	{// co dung
		$client = new Zend_Rest_Client(HTTP_SERVICE);
		$return = $client->restGet("/users/route/selectALLUser");
		$return = json_decode(json_encode((array) simplexml_load_string($return->getBody())),1);
		
		for ($i=0;$i <count($return);$i++)
				$data[$i] = $return['row_'.$i];
		if(count($data) > 0){
			$keys = array_keys($data[0]);
		
			for($i =0; $i < count($data) ;$i++){
				for($j =0 ;$j < count($keys);$j++){
					if(count($data[$i][$keys[$j]]) == 0)
						$data[$i][$keys[$j]] = "";
				}
			}
		}					
		return $data;		
	}

	public function selectUserByUserNamePassWord($name,$pass)
	{//co dung
		$client = new Zend_Rest_Client(HTTP_SERVICE);
		$return = $client->restGet("/users/route/selectUserByUserNamePassWord/name/".$name."/pass/".$pass);
		$return = json_decode(json_encode((array) simplexml_load_string($return->getBody())),1);
		
		for ($i=0;$i <count($return);$i++)
				$data[$i] = $return['row_'.$i];
		
		if(count($data) > 0){
			$keys = array_keys($data[0]);
		
			for($i =0; $i < count($data) ;$i++){
				for($j =0 ;$j < count($keys);$j++){
					if(count($data[$i][$keys[$j]]) == 0)
						$data[$i][$keys[$j]] = "";
				}
			}
		}		
		return $data;		
	}
	public function selectUserByUserName($name,$id)
	{// co dung
		$client = new Zend_Rest_Client(HTTP_SERVICE);
		$return = $client->restGet("/users/route/selectUserByUserName/name/".$name."/id/".$id);
		$return = json_decode(json_encode((array) simplexml_load_string($return->getBody())),1);
		
		for ($i=0;$i <count($return);$i++)
				$data[$i] = $return['row_'.$i];

		if(count($data) > 0){
			$keys = array_keys($data[0]);
		
			for($i =0; $i < count($data) ;$i++){
				for($j =0 ;$j < count($keys);$j++){
					if(count($data[$i][$keys[$j]]) == 0)
						$data[$i][$keys[$j]] = "";
				}
			}
		}				
		return $data;		
	}
	public function selectUserByEmail($mail,$id)
	{// co dung
		$client = new Zend_Rest_Client(HTTP_SERVICE);
		$return = $client->restGet("/users/route/selectUserByEmail/mail/".$mail."/id/".$id);
		$return = json_decode(json_encode((array) simplexml_load_string($return->getBody())),1);
		
		for ($i=0;$i <count($return);$i++)
				$data[$i] = $return['row_'.$i];

		if(count($data) > 0){
			$keys = array_keys($data[0]);
		
			for($i =0; $i < count($data) ;$i++){
				for($j =0 ;$j < count($keys);$j++){
					if(count($data[$i][$keys[$j]]) == 0)
						$data[$i][$keys[$j]] = "";
				}
			}
		}				
		return $data;		
	}
	public function selectUserByUserID($user_id)
	{// co dung
		$client = new Zend_Rest_Client(HTTP_SERVICE);
		$return = $client->restGet("/users/route/selectUserByUserID/user_id/".$user_id);
		$return = json_decode(json_encode((array) simplexml_load_string($return->getBody())),1);
		
		for ($i=0;$i <count($return);$i++)
				$data[$i] = $return['row_'.$i];

		if(count($data) > 0){
			$keys = array_keys($data[0]);
		
			for($i =0; $i < count($data) ;$i++){
				for($j =0 ;$j < count($keys);$j++){
					if(count($data[$i][$keys[$j]]) == 0)
						$data[$i][$keys[$j]] = "";
				}
			}
		}				
		return $data;		
	}
	public function selectUserByGroup($group_id)
	{// co dung
		$client = new Zend_Rest_Client(HTTP_SERVICE);
		$return = $client->restGet("/users/route/selectUserByGroup/group_id/".$group_id);
		$return = json_decode(json_encode((array) simplexml_load_string($return->getBody())),1);
		
		for ($i=0;$i <count($return);$i++)
				$data[$i] = $return['row_'.$i];

		if(count($data) > 0){
			$keys = array_keys($data[0]);
		
			for($i =0; $i < count($data) ;$i++){
				for($j =0 ;$j < count($keys);$j++){
					if(count($data[$i][$keys[$j]]) == 0)
						$data[$i][$keys[$j]] = "";
				}
			}
		}				
		return $data;		
	}
	
	////////////
	/* insert*/
	//////////
	public function insertUser($data)
	{// co dung
		$data['route'] = "insertUser";
		$client = new Zend_Rest_Client(HTTP_SERVICE);
		$return = $client->restPost("/users", $data);

		return $return->getBody();		
	}
	
	////////////
	/* update*/
	//////////
	public function updateUser($data)
	{// co dung		
		$client = new Zend_Rest_Client(HTTP_SERVICE);
		$data['address'] = str_replace("/", "~", $data['address']);
		$temp = serialize($data);
		$return = $client->restPut("/users/route/updateUser/data/".urlencode($temp));
		return $return->getBody();		
	}
	
	////////////
	/* delete*/
	//////////
	public function deleteUser($id)
	{// co dung
		$client = new Zend_Rest_Client(HTTP_SERVICE);
		$return = $client->restDelete("/users/route/deleteUser/id/".$id);
		return $return->getBody();		
	}
}